//
// Created by changlei on 2022/4/25.
//

import Foundation
import UIKit
import Kingfisher


typealias WillPerformPreviewAction = (_ interaction: UIContextMenuInteraction, _ configuration: UIContextMenuConfiguration, _ animator: UIContextMenuInteractionCommitAnimating) -> Void

class CoverView: UIImageView {
    lazy private var interaction: ContextMenuInteraction = {
        ContextMenuInteraction()
    }()

    override init(image: UIImage? = nil) {
        super.init(image: image)
        onInit()
    }

    override init(image: UIImage? = nil, highlightedImage: UIImage? = nil) {
        super.init(image: image, highlightedImage: highlightedImage)
        onInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        onInit()
    }

    private func onInit() {
        contentMode = .scaleAspectFill
        layer.cornerRadius = 5
        clipsToBounds = true
    }

    func setInteraction(_ contextMenuConfiguration: UIContextMenuConfiguration?, willPerformPreviewAction: WillPerformPreviewAction? = nil) {
        isUserInteractionEnabled = contextMenuConfiguration != nil
        interaction.contextMenuConfiguration = contextMenuConfiguration
        interaction.willPerformPreviewAction = willPerformPreviewAction
        removeInteraction(interaction.interaction)
        if contextMenuConfiguration != nil {
            addInteraction(interaction.interaction)
        }
    }

    public func setImage(with resource: String) {
        kf.setImage(with: URL(string: resource)!, placeholder: CoverPlaceholder())
    }
}

fileprivate class ContextMenuInteraction: NSObject, UIContextMenuInteractionDelegate {
    fileprivate var contextMenuConfiguration: UIContextMenuConfiguration?
    fileprivate var willPerformPreviewAction: WillPerformPreviewAction?

    lazy fileprivate var interaction: UIContextMenuInteraction = {
        UIContextMenuInteraction(delegate: self)
    }()

    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        contextMenuConfiguration
    }

    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
        if let willPerformPreviewAction = willPerformPreviewAction {
            willPerformPreviewAction(interaction, configuration, animator)
        }
    }
}

fileprivate class CoverPlaceholder: Placeholder {
    lazy var indicator: UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView()
        indicatorView.style = .medium
        return indicatorView
    }()

    func add(to imageView: KFCrossPlatformImageView) {
        imageView.addSubview(indicator)
        indicator.snp.makeConstraints { make in
            make.size.lessThanOrEqualToSuperview()
            make.center.equalToSuperview()
        }
    }

    func remove(from imageView: KFCrossPlatformImageView) {
        indicator.removeFromSuperview()
    }
}